package cn._2dland.leech.clients;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.NameValuePair;

import cn._2dland.leech.LeechClient;

/**
 * A leech client use gr0wl.com
 * @author deadblue
 *
 */
public class Gr0wlClient extends LeechClient {

	@Override
	protected String getParseURL(String url) {
		String parseURL = null;

		if(url.startsWith("http://hotfile.com/dl/")) {
			parseURL = "http://hf.gr0wl.com/index.php";
		} else if(url.startsWith("http://www.wupload.com/file/")) {
			parseURL = "http://wu.gr0wl.com/index.php";
		}

		return parseURL;
	}

	@Override
	protected String getParseReferer(String url) {
		String referer = null;

		if(url.startsWith("http://hotfile.com/dl/")) {
			referer = "http://hf.gr0wl.com/";
		} else if(url.startsWith("http://www.wupload.com/file/")) {
			referer = "http://wu.gr0wl.com/";
		}

		return referer;
	}

	@Override
	protected NameValuePair[] getParseParams(String url) {
		NameValuePair[] params = {
				new NameValuePair("link", url),
				new NameValuePair("premium_acc", "on")
		};
		return params;
	}

	@Override
	protected String getTransloadURL(String url) {
		return getParseURL(url);
	}

	@Override
	protected String parseDownloadURL(String url, String body) {
		String downURL = null;

		try {
			Matcher m = Pattern.compile("<a href=\"/dl/[^\"]+\"").matcher(body);
			if(m.find()) {
				downURL = body.substring(m.start() + 10, m.end() - 1);
				downURL = getParseReferer(url) + downURL;
			}
		} catch (Exception e) {
			e.printStackTrace(System.err);
		}

		return downURL;
	}

}
